package com.ruoyi;



import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ShopifyApiUtil {


    public static void main(String[] args) {
        createProduct();
    }

    public static void createProduct() {
        String apiUrl = "https://904df7-3.myshopify.com/admin/api/2024-01/products.json";
        String token = "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";
        JSONObject params = new JSONObject();
        params.put("title", "华为mate 60 pro");
        params.put("body_html", "这里添加一些html的标签图片吧");
        params.put("product_type", "手机");
        params.put("status", "active"); // 状态：active-活跃 draft-草稿
        params.put("metafields_global_title_tag", "seo的标题");
        params.put("metafields_global_description_tag", "seo的描述");

        JSONObject products = new JSONObject();
        products.put("product", params);
        String response = sendPost(apiUrl, token, products.toJSONString());
        System.out.println(response);
    }


    public static String sendPost(String urls, String token, String params) {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建HttpPost对象，设置URL和参数
        HttpPost httpPost = new HttpPost(urls);
        httpPost.addHeader("Content-Type", "application/json"); // 设置请求头，可以根据需要修改
        httpPost.addHeader("X-Shopify-Access-Token", token);
        // 设置请求参数，这里是一个JSON字符串
        httpPost.setEntity(new StringEntity(params, "UTF-8"));
        try {
            // 发送POST请求
            HttpResponse response = httpClient.execute(httpPost);
            // 获取响应内容
            HttpEntity entity = response.getEntity();
            String responseBody = EntityUtils.toString(entity);
            // 处理响应结果
            return responseBody;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

/*
响应数据：
        {
        "product": {
        "id": 7032839962647,
        "title": "华为mate 60 pro",
        "body_html": "这里添加一些html的标签图片吧",
        "vendor": "My Store",
        "product_type": "手机",
        "created_at": "2024-02-01T22:31:17-08:00",
        "handle": "华为mete-60-pro",
        "updated_at": "2024-02-01T22:31:17-08:00",
        "published_at": "2024-02-01T22:31:17-08:00",
        "template_suffix": null,
        "published_scope": "global",
        "tags": "",
        "status": "active",
        "admin_graphql_api_id": "gid:\/\/shopify\/Product\/7032839962647",
        "variants": [
        {
        "id": 40889038110743,
        "product_id": 7032839962647,
        "title": "Default Title",
        "price": "0.00",
        "sku": "",
        "position": 1,
        "inventory_policy": "deny",
        "compare_at_price": null,
        "fulfillment_service": "manual",
        "inventory_management": null,
        "option1": "Default Title",
        "option2": null,
        "option3": null,
        "created_at": "2024-02-01T22:31:17-08:00",
        "updated_at": "2024-02-01T22:31:17-08:00",
        "taxable": true,
        "barcode": null,
        "grams": 0,
        "image_id": null,
        "weight": 0.0,
        "weight_unit": "kg",
        "inventory_item_id": 42988266356759,
        "inventory_quantity": 0,
        "old_inventory_quantity": 0,
        "requires_shipping": true,
        "admin_graphql_api_id": "gid:\/\/shopify\/ProductVariant\/40889038110743"
        }
        ],
        "options": [
        {
        "id": 9025768292375,
        "product_id": 7032839962647,
        "name": "Title",
        "position": 1,
        "values": [
        "Default Title"
        ]
        }
        ],
        "images": [],
        "image": null
        }
        }


 */
